import React, { Component } from 'react';
import PropTypes from 'prop-types'; // 对接受的props进行限制
import {nanoid} from 'nanoid';

import './header.css'

export default class Header extends Component {
    
    //对接收的props进行：类型，必要性的限制
    static propTypes = {
        addTodo: PropTypes.func.isRequired
    }
    
    //鼠标按下触发
    handleKeyUp = (event) => {
        const { keyCode, target } = event;
        // 是否按下回车
        if (keyCode !== 13) return;
        if (target.value === '') {
            alert('任务名不能为空');
            return
        }
        // 添加todo
        this.props.addTodo({ id: nanoid(), name: target.value, done: false });
        target.value = '';
    }
    render() {
        return (
            <div className="todo-header">
                <input onKeyUp={this.handleKeyUp} type="text" placeholder="请输入你的任务名称，按回车键确认" />
            </div>
        )
    }
}
